﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace YellowMap
{
    public class RestService
    {


       
        public List<object> Attachments { get; set; }
        public string Url { get; set; }

        RestResponse response = new RestResponse();
        bool isDownloadCompleted;

        public RestService()
        {
            Attachments = new List<object>();
        }

        public async Task<RestResponse> SendAsync(string url = "")
        {
            if (url == "")
            {
                url = this.Url;
            }
            isDownloadCompleted = false;
            response.Attachments = Attachments;
            WebClient client = new WebClient();
            client.DownloadStringCompleted += client_DownloadStringCompleted;
            client.DownloadStringAsync(new Uri(url));

            while (!isDownloadCompleted)
            {
                await Task.Delay(100);
            }

            return response;


        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            isDownloadCompleted = true;
            response.Error = e.Error;
            if (e.Error == null)
            {
                response.Result = e.Result;
            }
        }

    }
}
